package com.example.myapplication11_10;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity {
    private CandleStickChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_candle);

        /*第一步实例化*/
        chart = findViewById(R.id.chart);
        setTitle("k线图");

        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);

        /*2. */


        ArrayList <CandleEntry>list=new ArrayList<>();
        for (int i=0;i<10;i++){ //
            float val=(float) (Math.random()*40);
            float high=(float)(Math.random()*9)+8f;//最高价
            float low=(float)(Math.random()*9)+8f;//最低价
            float open=(float)(Math.random()*6)+1f;//开盘价
            float close=(float)(Math.random()*6)+1f;//关盘价
            boolean even=i%2==0;
            list.add(new CandleEntry(
                    i,val+high,
                    val-low,
                    even?val+open:val-open,
                    even?val-close:val+close
            ));
        }
        /*x轴*/
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        /*Y轴*/
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(7, false);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawAxisLine(false);

        YAxis rightAxis = chart.getAxisLeft();
        rightAxis.setEnabled(false);

        CandleDataSet set1=new CandleDataSet(list,"set");
        set1.setDrawIcons(false);
        set1.setShadowColor(Color.GRAY);
        set1.setShadowWidth(1.2f);
        set1.setDecreasingColor(Color.RED); //降色
        set1.setIncreasingColor(Color.GREEN); //增色
        set1.setDecreasingColor(Color.RED);//填充颜色
        set1.setDecreasingPaintStyle(Paint.Style.FILL);//填充


        CandleData data=new CandleData(set1);
        chart.setData(data);
        chart.invalidate();

    }
}
